

# Random Effect Canton
raw.re.canton <- ranef(model.r)$canton
namesCan <- rownames(raw.re.canton)
raw.re.canton <- cbind(as.numeric(namesCan), raw.re.canton)
re.canton <- rep(0,26)
	for (t in raw.re.canton[,1]){
		re.canton[t] <- raw.re.canton[raw.re.canton[,1]==t,2]
	}

# Random Effect Education
raw.re.educ <- ranef(model.r)$x1.education
namesEduc <- rownames(ranef(model.r)$x1.education)
raw.re.educ <- cbind(as.numeric(namesEduc),raw.re.educ)
re.educ <- rep(0,6)
	for (t in raw.re.educ[,1]){
		re.educ[t] <- raw.re.educ[raw.re.educ[,1]==t,2]
	}
	
# Random Effect Region
raw.re.reg <- ranef(model.r)$region
namesReg <- rownames(raw.re.reg)
raw.re.reg <- cbind(as.numeric(namesReg), raw.re.reg)
re.reg <- rep(0,7)
	for (t in raw.re.reg[,1]){
		re.reg[t] <- raw.re.reg[raw.re.reg[,1]==t,2]
	}

# Random Effect Age
raw.re.age <- ranef(model.r)$x1.age
namesAge <- rownames(raw.re.age)
raw.re.age <- cbind(as.numeric(namesAge), raw.re.age)
re.age <- rep(0,4)
	for (t in raw.re.age[,1]){
		re.age[t] <- raw.re.age[raw.re.age[,1]==t,2]
	}
	
# Random Effect Gender
re.female1 <- ranef(model.r)$x1.gender
re.female <- rep(NA,2)
re.female[1] <- re.female1[[1]][1]
re.female[2] <- re.female1[[1]][2]

# Random Effect Party- ID
raw.re.party <-  ranef(model.r)$x1.partyL
namesParty <- rownames(raw.re.party)
raw.re.party <- cbind(as.numeric(namesParty), raw.re.party)
re.party <- rep(0,7)
	for (t in raw.re.party[,1]){
		re.party[t] <- raw.re.party[raw.re.party[,1]==t,2]
	}



# Read-in context data in small data form (only 26 rows)
data3 <- read.dta("Contextdata1.dta")
jjj <- data.run$JAHR[1]
data3 <- data3[data3$JAHR==jjj,]
X21 <- data3$leftvote_share
X22 <- data3$romkath_share
X23 <- data3$german_share
X24 <- data3$leftvote_share
#X25 <- data3$svpvote_share
#X26 <- data3$foreign_share
#X27 <- data3$oeffbesch_share
region.pointer <- data3$region
regional.re <- rep(NA,26)
for (r in 1:26){
	regional.re[r] <- re.reg[region.pointer[r]]
}



mod.list <- list(c(3,2), c(3,2))
#mod.list <- list(c(4), c(4))

XX <- cbind(X21,X22,X23,X24) #,X25,X26,X27)
#ifelse(w==1, xLx <- 1, xLx <- cbind(1,XX[,mod.list[[w]]]))
xLx <- cbind(1,XX[,mod.list[[w]]])
#xLx <- as.matrix(rep(1,length(X21)))
pred.cantonal <- xLx%*%fixef(model.r) + re.canton + regional.re


# type-specific prediction --- synthetic joint has 336 ideal types; EDUC(6), AGE(4), FEMALE(2), PARTY(7)

educ.mark <- c(rep(1,56),rep(2,56),rep(3,56),rep(4,56),rep(5,56),rep(6,56))
age.mark <- rep(c(rep(1,14),rep(2,14),rep(3,14),rep(4,14)),6)
female.mark <- rep(c(rep(1,7),rep(2,7)),24)
party.mark <- rep(c(1:7),48)
block.indiv <- rep(NA,336)
	for (t in 1:336){
		block.indiv[t] <- re.educ[educ.mark[t]] + re.age[age.mark[t]] + re.female[female.mark[t]] + re.party[party.mark[t]]
	}


##############################################################################################################################

# read-in census data
#setwd("/Users/lleemann/Dropbox/Democratic Deficit exchange folder/BfS")
#block <- read.csv("census final BfS 11 2012-- manip v2.0.csv", header = TRUE)

# Save the first line of each canton
N <- rep(NA,26)
for (i in 1:26){
	n <- 3+i*2411
	N[i] <- n	
}
N <- c(N,65100)

# create container, voter types disaggregated * cantons
pull.out.BIG <- matrix(NA,12,26)

# read out canton by canton, creating "pull.out.BIG" (192*26), for every type and every canton a head count
for (k in 1:26){
	eins <- k
	zwei <- k+1
	block.use <- as.matrix(block[(N[eins]):(N[zwei]-1),])
	# here is all the hard work by hand
	source("census_pull_out_command_CH_marginal.R")
	pull.out.BIG[,k] <- pull.out.d
}

### Adding party proportions porportions to "pull.out.BIG"
data.party <- read.dta("parties in cantons.dta")
data.party <- data.party[data.party$JAHR==data.run$JAHR[1],]
party.shares <- data.party[,c(39:44)]

# ! #
ratioL <- 0.8			# share of population which belongs to party
party.shares <- cbind(party.shares*ratioL,1-ratioL)
# ! #


pull.out.BIG <- rbind(pull.out.BIG,t(party.shares))
pull.out.BIG.educ <- pull.out.BIG[1:6,]
pull.out.BIG.age  <- pull.out.BIG[7:10,]
pull.out.BIG.female  <- pull.out.BIG[11:12,]
pull.out.BIG.party  <- pull.out.BIG[13:19,]

# standard
	if(w==1){
		# synthetic joint for 336*26
		pull.out.synth <- rep(NA, 336*26)
			for (q in 1:26){
				aa <- 1+((q-1)*336)
				bb <- (q*336)
				synthi <- rep(NA, 336)
				for (k in 1:336){
					synthi[k] <- pull.out.BIG.educ[educ.mark[k],q] * pull.out.BIG.age[age.mark[k],q] * pull.out.BIG.female[female.mark[k],q] * pull.out.BIG.party[party.mark[k],q]
				}
			synthiP <- synthi/sum(synthi)
			pull.out.synth[aa:bb] <- synthiP
			}
	}
# use survey information in party
	if(w==2){
		# synthetic joint for 336*26
		pull.out.synth <- rep(NA, 336*26)
			for (q in 1:26){
				aa <- 1+((q-1)*336)
				bb <- (q*336)
				synthi <- rep(NA, 336)
				for (k in 1:336){
					synthi[k] <- pull.out.BIG.educ[educ.mark[k],q] * pull.out.BIG.age[age.mark[k],q] * pull.out.BIG.female[female.mark[k],q] 
				}
			# Now, adjusting survey information with true marginal party information
			party.canton.vec <- pull.out.BIG.party[,q]	
			party.survey.shares <- colSums(party.ideal)/sum(party.ideal)
			party.surv.corr <- party.canton.vec/party.survey.shares
			party.ideal.corr <- party.ideal %*% diag(party.surv.corr)
			# could add multinomial draw here
			pi.336.vec <- c(t(party.ideal.corr))
			full.336 <- synthi * pi.336.vec
			full.336 <- full.336/sum(full.336) 
			pull.out.synth[aa:bb] <- full.336
			}
	}






# combining individual and contextual level
	A <- c(NA)
	for (s in 1:26){
		tata <- rep(pred.cantonal[s],336)
		A <- c(A,tata)	
	}
	y.lat.cont  <- A[-1]

	y.lat.ALL <- y.lat.cont + rep(block.indiv,26)
	y.predicted <- pnorm(y.lat.ALL)

	y.predicted.weighted <- pull.out.synth * y.predicted

	prediction.X <- rep(NA,26)
	for (q in 1:26){
		aa <- 1+((q-1)*336)
		bb <- (q*336)
		prediction.X[q] <- sum(y.predicted.weighted[aa:bb])
	}

predictionLMRP <- prediction.X
#print(prediction)
